package com.glacier.yuyuan.modules.admin.sys.web;

import cn.hutool.core.util.StrUtil;
import com.glacier.common.core.domain.Page;
import com.glacier.common.core.domain.Result;
import com.glacier.common.core.domain.form.ListWrapper;
import com.glacier.common.core.factory.TreeFactory;
import com.glacier.yuyuan.common.web.BaseController;
import com.glacier.yuyuan.modules.admin.sys.domain.Office;
import com.glacier.yuyuan.modules.admin.sys.service.OfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * date 2023-01-06 10:50
 * 组织机构控制层
 *
 * @author glacier
 * @version 1.0
 */
@RequestMapping("${yuyuan.adminPath}/sys/office")
@Controller
public class OfficeController extends BaseController {
    private OfficeService officeService;

    @Autowired
    public void setOfficeService(OfficeService officeService) {
        this.officeService = officeService;
    }

    @GetMapping("")
    public String list() {
        return "admin/sys/office/officeList";
    }

    @ResponseBody
    @PostMapping("/data")
    public Result<List<Office>> data(Office office) {
        List<Office> list = officeService.findList(office);
        return Result.ok("ok", list, list.size());
    }

    @ResponseBody
    @PostMapping("/page")
    public Result<List<Office>> page(Page<Office> page, Office office) {
        page = officeService.findPage(page, office);
        return Result.ok("ok", page.getData(), page.getTotal());
    }

    @ResponseBody
    @GetMapping("/tree")
    public List<Office> tree(Office office) {
        return TreeFactory.buildTree(officeService.findList(office));
    }

    @GetMapping("/form")
    public String form(Office office, Model model) {
        if (StrUtil.isNotBlank(office.getId())) {
            office = officeService.findById(office.getId());
        }
        model.addAttribute("entity", office);
        return "admin/sys/office/officeForm";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result<String> save(Office office) {
        officeService.save(office);
        return Result.ok("操作成功！", "");
    }

    @ResponseBody
    @PostMapping("/del")
    public Result<String> del(ListWrapper<String> form) {
        officeService.batchDelete(form.getDatas());
        return Result.ok("操作成功", "");
    }
}
